package top.hkyzf.study.leetcode.array;

import org.junit.Test;

import java.util.Arrays;

/**
 * @author 朱峰
 * @date 2022-1-21 9:37
 */
public class 移动零_283 {
    public void moveZeroes(int[] nums) {
        int left = 0; // 指向处理过的数组尾部
        int right = 0; // 指向未处理的数组头部
        int n = nums.length;
        int temp;
        if (n > 1) {
            while (right < n) {
                if (nums[right] != 0) {
                    temp = nums[left];
                    nums[left] = nums[right];
                    nums[right] = temp;
                    left++;
                }
                right++;
            }
        }
    }

    public void moveZeroes1(int[] nums) {
        int left = 0;
        int right = 0;
        int n = nums.length;
        while (right < n) {
            if(nums[right] != 0) {
                nums[left] = nums[right];
                right++;
                left++;
            } else {
                right++;
            }
        }
        for (int i = left; i < n; i++) {
            nums[i] = 0;
        }
    }

    @Test
    public void testMoveZeroes () {
        int[] nums = {0,1,0,3,12};
        moveZeroes1(nums);
        System.out.println(Arrays.toString(nums));
    }
}
